package com.juyu.miliao.scheduler.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.juyu.miliao.scheduler.domin.User;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Binding.DestinationType;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.support.converter.ContentTypeDelegatingMessageConverter;
import org.springframework.amqp.support.converter.DefaultClassMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.SimpleMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ErrorHandler;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private RabbitProperties props;

    @Bean
    public CachingConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(props.getHost(), props.getPort());
        connectionFactory.setUsername(props.getUsername());
        connectionFactory.setPassword(props.getPassword());
        connectionFactory.setPublisherConfirms(true);
        connectionFactory.setPublisherReturns(true);
        connectionFactory.setChannelCacheSize(100);
        return connectionFactory;
    }

//    @Bean
//    public RabbitAdmin rabbitAdmin() {
//        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory());
//        rabbitAdmin.setIgnoreDeclarationExceptions(false);
//        rabbitAdmin.declareQueue(new Queue("resetUserListCacheQueue", false));
//        rabbitAdmin.declareBinding(new Binding("resetUserListCacheQueue", DestinationType.QUEUE, "updateUserInfoExchange", "", null));
//        return rabbitAdmin;
//    }

    @Bean
    public SimpleMessageConverter simpleMessageConverter() {
        SimpleMessageConverter simpleMessageConverter = new SimpleMessageConverter();
        simpleMessageConverter.setDefaultCharset("UTF-8");
        return simpleMessageConverter;
    }

    @Bean
    public Jackson2JsonMessageConverter jackson2JsonMessageConverter() {
        Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter(objectMapper);
        return jackson2JsonMessageConverter;
    }

    @Bean
    public ContentTypeDelegatingMessageConverter contentTypeDelegatingMessageConverter() {
        ContentTypeDelegatingMessageConverter converter = new ContentTypeDelegatingMessageConverter();
        converter.addDelegate(MessageProperties.CONTENT_TYPE_TEXT_PLAIN, simpleMessageConverter());
        converter.addDelegate(MessageProperties.CONTENT_TYPE_JSON, jackson2JsonMessageConverter());
        return converter;
    }

    @Bean
    public ErrorHandler errorHandler() {
        return new RabbitMQExceptionHandler();
    }

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        factory.setMessageConverter(contentTypeDelegatingMessageConverter());
        factory.setErrorHandler(errorHandler());
        return factory;
    }

    @Bean
    public Queue videoQueue() {
        return new Queue("home.video.id", true);
    }

    @Bean
    public Queue UserQueue() {
        return new Queue("home.user.id", true);
    }
}